package net.sisgac.cliente.service;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import net.sisgac.cliente.model.Cliente;
import net.sisgac.cliente.model.dao.ClienteDAO;
import net.sisgac.cliente.model.dao.jpa.JPAClienteDAO;


public class ClienteServiceImpl implements ClienteService{
	private final String UNIDAD_DE_PERSISTENCIA ="jpaPersistence";
	private EntityManager em;
	private EntityManagerFactory fabrica;
	private ClienteDAO clienteDAO;
	
	public ClienteServiceImpl (){
		 em =	this.getEntityManager();
		clienteDAO = new JPAClienteDAO(em);
	}

	public Cliente buscarCliente(Cliente o){
		Cliente cliente=clienteDAO.find(o);
		if(cliente==null){
			throw new RuntimeException("Cliente no existe");
		}
		return cliente;
		
	}
	public EntityManager getEntityManager(){

		if(fabrica==null)
			fabrica = Persistence.createEntityManagerFactory(UNIDAD_DE_PERSISTENCIA);
		
		return fabrica.createEntityManager();
		
	}
	public Cliente buscarClienteID(String o){
		Cliente cliente=clienteDAO.findById(o);
		if(cliente==null){
			throw new RuntimeException("Cliente no existe");
		}return cliente;
	}
	
	public List<Cliente> listarClientes(){
		return clienteDAO.findAll();
	}

	
	

}
